import type { Ref, InjectionKey, UnwrapNestedRefs } from "vue";
import type {
  ValidatorErrorResult,
  Rule,
  Values,
  Rules,
  ValidateErrorResult,
} from "../validate";

export type State = "normal" | "validating" | "success" | "failure";

export interface FormField {
  name: string;
  setError: (messages: ValidatorErrorResult) => void;
  validate: () => Promise<any>;
  reset: () => void;
}

export interface FormEvent {
  onFocus: { name: string; value: any };
  onBlur: { name: string; value: any };
  onChange: { name: string; value: any };
  onReset: { name: string };
  onValidator: { name: string; messages: ValidatorErrorResult };
}

export interface Form {
  addField: (field: FormField) => void;
  removeField: (field: FormField) => void;
  getRule: (name: string) => Rule;
  setErrors: (errors: ValidateErrorResult) => void;
  state: Ref<State>;
  values: UnwrapNestedRefs<Values>;
  rules: UnwrapNestedRefs<Rules>;
}

export const formInjectKey = Symbol("form") as InjectionKey<Form>;
export const fieldInjectKey = Symbol("field") as InjectionKey<FormField>;
